# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=swig
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.1.1
pkgrel=3
pkgdesc="Generate scripting interfaces to C/C++ code (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="http://www.swig.org/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-pcre2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python"
              "${MINGW_PACKAGE_PREFIX}-lua"
              "${MINGW_PACKAGE_PREFIX}-ruby")
source=(https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz
        pathtools.c
        pathtools.h
        001-relocate.patch
        002-fix-python-find.patch)
sha256sums=('2af08aced8fcd65cdb5cc62426768914bedc735b1c250325203716f78e39ac9b'
            '08209cbf1633fa92eae7e5d28f95f8df9d6184cc20fa878c99aec4709bb257fd'
            '965d3921ec4fdeec94a2718bc2c85ce5e1a00ea0e499330a554074a7ae15dfc6'
            'a27041d7b13a17547f8a534d80c370a9e689f8f2784d150fc755cbb91382dac4'
            '057f0a306aee0d28a5bb6538b569c650e47c05b9848c9477cdeb7bb8c792944d')

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.[ch] Source/Swig/

  patch -p1 -i ${srcdir}/001-relocate.patch
  patch -p1 -i ${srcdir}/002-fix-python-find.patch

  ./autogen.sh
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-${pkgver}/configure\
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --without-perl5 \
    --without-guile \
    --without-python \
    --with-python3=${MINGW_PREFIX}/bin/python

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make -k check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE-UNIVERSITIES "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE-UNIVERSITIES
}
